package com.zycfc.zsf.boot.util.time;

import org.apache.commons.lang3.time.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class TimeUtil
{
    public static String getCurTimeStamp(final String format) {
        final Date date = new Date();
        final String strStamp = DateFormatUtils.format(date, format);
        return strStamp;
    }
    
    public static String ADD_DATE(final int optype, final String date, final int num) {
        String st_return = "";
        try {
            final DateFormat daf_date = DateFormat.getDateInstance(2, Locale.CHINA);
            daf_date.parse(date);
            final Calendar calendar = daf_date.getCalendar();
            calendar.add(optype, num);
            if (optype == 2) {
                calendar.add(5, -1);
            }
            String st_m = "";
            String st_d = "";
            final int y = calendar.get(1);
            final int m = calendar.get(2) + 1;
            final int d = calendar.get(5);
            if (m <= 9) {
                st_m = "0" + m;
            }
            else {
                st_m = "" + m;
            }
            if (d <= 9) {
                st_d = "0" + d;
            }
            else {
                st_d = "" + d;
            }
            st_return = y + "-" + st_m + "-" + st_d;
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return st_return;
    }
    
    private static String ADD_DATE2(final int optype, final String date, final int num) {
        String st_return = "";
        try {
            final DateFormat daf_date = DateFormat.getDateInstance(2, Locale.CHINA);
            daf_date.parse(date);
            final Calendar calendar = daf_date.getCalendar();
            calendar.add(optype, num);
            if (optype == 2) {
                calendar.add(5, 0);
            }
            String st_m = "";
            String st_d = "";
            final int y = calendar.get(1);
            final int m = calendar.get(2) + 1;
            final int d = calendar.get(5);
            if (m <= 9) {
                st_m = "0" + m;
            }
            else {
                st_m = "" + m;
            }
            if (d <= 9) {
                st_d = "0" + d;
            }
            else {
                st_d = "" + d;
            }
            st_return = y + "-" + st_m + "-" + st_d;
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return st_return;
    }
    
    public static String ADD_DAY(final String date, final int n) {
        return ADD_DATE(5, date, n);
    }
    
    public static String ADD_MONTH(final String date, final int n) {
        return ADD_DATE(2, date, n);
    }
    
    public static String ADD_MONTH2(final String date, final int n) {
        return ADD_DATE2(2, date, n);
    }
    
    public static String ADD_YEAR(final String date, final int n) {
        return ADD_DATE(1, date, n);
    }
    
    public static boolean validate(final String dateString) {
        final Pattern p = Pattern.compile("\\d{4}+[-]\\d{1,2}+[-]\\d{1,2}+");
        final Matcher m = p.matcher(dateString);
        if (!m.matches()) {
            return false;
        }
        final String[] array = dateString.split("-");
        final int year = Integer.valueOf(array[0]);
        final int month = Integer.valueOf(array[1]);
        final int day = Integer.valueOf(array[2]);
        if (month < 1 || month > 12) {
            return false;
        }
        final int[] monthLengths = { 0, 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        if (isLeapYear(year)) {
            monthLengths[2] = 29;
        }
        else {
            monthLengths[2] = 28;
        }
        final int monthLength = monthLengths[month];
        return day >= 1 && day <= monthLength;
    }
    
    private static boolean isLeapYear(final int year) {
        return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
    }
}
